﻿#region Using directives

using System;
using System.ComponentModel;
using System.Collections;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
#endregion

namespace SPL.Entities
{	
	///<summary>
	/// An object representation of the 'KVT' table. [No description found the database]	
	///</summary>
	/// <remarks>
	/// This file is generated once and will never be overwritten.
	/// </remarks>	
  [Serializable, DataObject]
  [CLSCompliant(true)]
	public partial class Kvt : KvtBase
	{
    #region private data
    private bool _isLoadPreData = false;
    private double _pre7 = 0;
    private double _pre8 = 0;
    private double _pre9 = 0;
    private double _pre21 = 0;
    private double _pre30 = 0;
    private double _pre32 = 0;
    private double _pre33 = 0;
    private double _pre34 = 0;
    private double _pre39 = 0;
    private double _pre40 = 0;
    private double _pre41 = 0;
    private double _pre42 = 0;

    #endregion

		#region Constructors

		///<summary>
		/// Creates a new <see cref="Kvt"/> instance.
		///</summary>
		public Kvt():base(){}	
		
		#endregion

    #region Custom fields
    /// <summary>
    /// 
    /// </summary>
    [DescriptionAttribute(""), System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)]
    [DataObjectField(false, false, false)]
    public virtual System.String I1InputDate0hString
    {
      get
      {
        return this.I1InputDate.ToString("dd/MM/yyyy");
      }
    }

    /// <summary>
    /// 
    /// </summary>
    [DescriptionAttribute(""), System.ComponentModel.Bindable(System.ComponentModel.BindableSupport.Yes)]
    [DataObjectField(false, false, false)]
    public virtual System.String I1InputDate24hString
    {
      get
      {
        return this.I1InputDate.AddDays(1.0).ToString("dd/MM/yyyy");
      }
    }

   
    #region pre data
    /// <summary>
    /// 
    /// </summary>
    public virtual double pre7
    {
      get
      {
        if (!_isLoadPreData)
        {
          LoadPreData();
          _isLoadPreData = true;
        }
        return _pre7;
      }
    }   
    #endregion

    #endregion

    #region Helper method

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    private void LoadPreData()
    {
      TimeSpan oneDay = new TimeSpan(1, 0, 0, 0, 0);
      DateTime yesterday = this.I1InputDate.Subtract(oneDay);

      string queryString = "SELECT * FROM TVT WHERE I1InputDate='" + yesterday.ToShortDateString() + "' AND Hid IS NULL";
      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["SPL.Data.ConnectionString"].ConnectionString))
      {
        SqlCommand command = new SqlCommand(queryString, connection);
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        try
        {
          if (reader.Read())
          {
            //_pre7 = (double)reader["I7Fiq0401"];
            //_pre8 = (double)reader["I8Fiq0301"];
            //_pre9 = (double)reader["I9Fiq0302"];
            //_pre21 = (double)reader["I21Fiq2402"];
            //_pre30 = (double)reader["I30KctvNhanCond"];
            //_pre32 = (double)reader["I32Me13a"];
            //_pre33 = (double)reader["I33Me13b"];
            //_pre34 = (double)reader["I34Fiq5801"];
            //_pre39 = TinhKhoiLuong1((double)reader["I35v21aMucBon"]);
            //_pre40 = TinhKhoiLuong1((double)reader["I36v21bMucBon"]);
            //_pre41 = TinhKhoiLuong1((double)reader["I37v21cMucBon"]);
            //_pre42 = TinhKhoiLuong2((double)reader["I38Tk21MucBon"]);
          }
          else
          {
            //_pre7 = 0;
            //_pre8 = 0;
            //_pre9 = 0;
            //_pre21 = 0;
            //_pre30 = 0;
            //_pre32 = 0;
            //_pre33 = 0;
            //_pre34 = 0;
            //_pre39 = 0;
            //_pre40 = 0;
            //_pre41 = 0;
            //_pre42 = 0;
          }
        }
        catch
        {
          //_pre7 = 0;
          //_pre8 = 0;
          //_pre9 = 0;
          //_pre21 = 0;
          //_pre30 = 0;
          //_pre32 = 0;
          //_pre33 = 0;
          //_pre34 = 0;
          //_pre39 = 0;
          //_pre40 = 0;
          //_pre41 = 0;
          //_pre42 = 0;
        }
        finally
        {
          // Always call Close when done reading.
          reader.Close();
        }
      }
    }
    private double TinhKhoiLuong1(double muc_bon)
    {
      try
      {
        double temp = (1.675 - 3.35 * muc_bon / 100) / 1.675;
        return (Math.Round(54.61 * 0.541 * (1.675 * 1.675 / 2 * ((Math.Acos(temp) * 2) - (Math.Sin(Math.Acos(temp) * 2)))), 3));
      }
      catch
      {
        return 0;
      }

    }
    private double TinhKhoiLuong2(double muc_bon)
    {
      try
      {
        return (Math.Round((15.6 / 100 * muc_bon) * 13 * 13 / 4 * Math.PI * 0.666, 3));
      }
      catch
      {
        return 0;
      }
    }
    #endregion


	}
}
